package com.jacky.iot.handler;

import com.jacky.iot.proto.TaskProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ServerProtobufHandler extends SimpleChannelInboundHandler<TaskProto.TaskProtocol> {
    private static final Logger LOG = LoggerFactory.getLogger(ServerProtobufHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, TaskProto.TaskProtocol tp) throws Exception {
        switch (tp.getPackType()) {
            case LOGIN ->
                    LOG.info("服务端收到[登录]请求:username:{},password:{}", tp.getLoginPack().getUsername(), tp.getLoginPack().getPassword());
            case CREATE_TASK ->
                    LOG.info("服务端收到[创建任务]请求:taskId:{},taskName:{}", tp.getCreateTaskPack().getTaskId(), tp.getCreateTaskPack().getTaskName());
            case DELETE_TASK -> LOG.info("服务端收到[删除任务]请求:taskId:{}", tp.getDeleteTaskPack().getTaskIdList());
            default -> LOG.info("服务端收到[未知类型]请求:taskType:{}", tp.getPackType());
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        LOG.error("服务端发生异常:", cause);
    }
}
